package com.cuit.blog.service;
import com.cuit.blog.dao.ArticleRespository;
import com.cuit.blog.dao.ArticletypeRespository;
import com.cuit.blog.entity.Article;
import com.cuit.blog.entity.Articletype;
import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
@Transactional
public class ArticleService {
    @Autowired
    ArticleRespository articleRespository;
    @Autowired
    ArticletypeRespository articletypeRespository;
    public List<Article> findAll()  {
	return articleRespository.findAll();
    }
    public Page<Article> findAllByPage(int pageIndex, int pageSize) {
	Sort sort = Sort.by(Sort.Direction.DESC, "id");
	Pageable pageable = PageRequest.of(pageIndex, pageSize, sort);
	Page<Article> articlePage = articleRespository.findAll(pageable);
	return articlePage;
    }
    public Page<Article> pageArticleByType(String name, int pageIndex, int pageSize) {
	Sort sort = Sort.by(Sort.Direction.ASC, "id");
	Pageable pageable = PageRequest.of(pageIndex, pageSize, sort);
	Page<Article> articlePage = articleRespository.findByTitle(name, pageable);
	return articlePage;
    }
    public List<Article> findByTitleLike(String title) {
	return articleRespository.findByTitleLike(title);
    }
    public Article save(Article article) {
	return articleRespository.save(article);
    }
    public Article findById(long id) {
	return articleRespository.findById(id).orElse(new Article());
    }
    public void deleteById(long id) {
	articleRespository.deleteById(id);
    }
    public void updateById(String type, long id) {
	articletypeRespository.updateById(type, id);
    }
}
